package com.toolbox.controller;

import com.toolbox.service.ToolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/tools")
public class ToolController {

    @Autowired
    private ToolService toolService;

    /**
     * 文档转换 (兼容旧版API)
     */
    @PostMapping("/document/convert_old")
    public ResponseEntity<?> convertDocument(@RequestParam("file") MultipartFile file,
                                            @RequestParam("targetFormat") String targetFormat) {
        try {
            byte[] convertedFile = toolService.convertDocument(file, targetFormat);
            String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf('.')) + "." + targetFormat;
            return ResponseEntity.ok()
                    .header("Content-Disposition", "attachment; filename=" + fileName)
                    .body(convertedFile);
        } catch (Exception e) {
            Map<String, String> errorMap = new HashMap<>();
            errorMap.put("error", e.getMessage());
            return ResponseEntity.badRequest().body(errorMap);
        }
    }

    /**
     * 证件照背景处理 (兼容旧版API)
     */
    @PostMapping("/photo/background_old")
    public ResponseEntity<?> changeBackground(@RequestParam("file") MultipartFile file,
                                             @RequestParam("backgroundType") String backgroundType) {
        try {
            byte[] processedImage = toolService.changePhotoBackground(file, backgroundType);
            try {
                // 按照RFC 5987标准编码中文文件名
                String encodedFileName = java.net.URLEncoder.encode("processed_" + file.getOriginalFilename(), "UTF-8")
                        .replace("+", "%");
                return ResponseEntity.ok()
                        .header("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName)
                        .body(processedImage);
            } catch (Exception e) {
                // 编码失败时使用默认文件名
                return ResponseEntity.ok()
                        .header("Content-Disposition", "attachment; filename=processed_image.jpg")
                        .body(processedImage);
            }
        } catch (Exception e) {
            Map<String, String> errorMap = new HashMap<>();
            errorMap.put("error", e.getMessage());
            return ResponseEntity.badRequest().body(errorMap);
        }
    }

    /**
     * 英文翻译
     */
    @PostMapping("/translate")
    public ResponseEntity<?> translateText(@RequestBody Map<String, String> request) {
        try {
            String text = request.get("text");
            String sourceLang = request.getOrDefault("sourceLang", "auto");
            String targetLang = request.getOrDefault("targetLang", "zh-CN");
            
            String translatedText = toolService.translateText(text, sourceLang, targetLang);
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("translatedText", translatedText);
            return ResponseEntity.ok(resultMap);
        } catch (Exception e) {
            Map<String, String> errorMap = new HashMap<>();
            errorMap.put("error", e.getMessage());
            return ResponseEntity.badRequest().body(errorMap);
        }
    }

    /**
     * PPT生成 (兼容旧版API)
     */
    @PostMapping("/ppt/generate_old")
    public ResponseEntity<?> generatePPT(@RequestBody Map<String, Object> request) {
        try {
            byte[] pptFile = toolService.generatePPT(request);
            String fileName = "generated_ppt_" + System.currentTimeMillis() + ".pptx";
            return ResponseEntity.ok()
                    .header("Content-Disposition", "attachment; filename=" + fileName)
                    .body(pptFile);
        } catch (Exception e) {
            Map<String, String> errorMap = new HashMap<>();
            errorMap.put("error", e.getMessage());
            return ResponseEntity.badRequest().body(errorMap);
        }
    }
}